package rjeutsky.database.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import rjeutsky.database.interfaces.CRUDDomainRepository;
import rjeutsky.models.Subscription;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;

public class SubscriptionDomainRepository implements CRUDDomainRepository<Subscription> {

	private static final String CREATE_SQL = "INSERT INTO Subscription (Id, UserId) VALUES (?, ?)";
	private static final String CREATE_NO_ID_SQL = "INSERT INTO Subscription (UserId) VALUES (?)";
	
	private PreparedStatement createStatement;
	private PreparedStatement createNoIdStatement;
	
	public SubscriptionDomainRepository(Connection connection) throws SQLException {
		createStatement = connection.prepareStatement(CREATE_SQL, PreparedStatement.RETURN_GENERATED_KEYS);
		createNoIdStatement = connection.prepareStatement(CREATE_NO_ID_SQL, PreparedStatement.RETURN_GENERATED_KEYS);
	}
	
	@Override
	public void create(Subscription item) throws SQLException {
		if (item.getId() == null) {
			createNoIdStatement.setLong(1, item.getUserId());
			createNoIdStatement.executeUpdate();
			ResultSet generatedKeys = createNoIdStatement.getGeneratedKeys();
			generatedKeys.next();
			item.setId(generatedKeys.getLong(1));
		}
		else {
			createStatement.setLong(1, item.getId());
			createStatement.setLong(2, item.getUserId());
			createStatement.executeUpdate();
		}
	}

	@Override
	public void delete(long id) {
		// TODO Auto-generated method stub
		throw new NotImplementedException();
	}

	@Override
	public Subscription read(long id) {
		// TODO Auto-generated method stub
		throw new NotImplementedException();
	}

	@Override
	public void update(Subscription item) {
		// TODO Auto-generated method stub
		throw new NotImplementedException();
	}

	@Override
	public List<Subscription> query() {
		// TODO Auto-generated method stub
		throw new NotImplementedException();
	}

}
